package dao_stanza;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;


import bean.Stanza;
import bean.Utente;

import util.EntityManagerUtil;
 
public class GestoreStanza implements StanzaInterface{
	
	 public GestoreStanza(){
		    manager = EntityManagerUtil.getInstance();
		    }
		    @Override
		    public void persist(bean.Stanza stanza){
		       EntityTransaction tn=manager.getTransaction();
		       tn.begin();
		       manager.persist(stanza);
		       tn.commit();
		       
		    }
		    
			@Override
			public ArrayList<Stanza> getStanze(Integer id_casa) {
				EntityTransaction tn=this.manager.getTransaction();
		    	ArrayList<bean.Stanza> stanze;
		    	tn.begin();
		    	try{
		    	Query q=manager.createQuery("SELECT s FROM Stanza s WHERE s.casa.id_casa=?");
		    	q.setParameter(1, id_casa);
		    	
		    	stanze=new ArrayList<bean.Stanza>(q.getResultList());
		    	}catch(NullPointerException e){
		    		tn.commit();
		    		return null;
		    	}
		    	 tn.commit();
		    	return stanze;
			}

			@Override
			public Stanza findStanzabyId(Integer id_stanza) {
				EntityTransaction tn=manager.getTransaction();
				tn.begin();
				bean.Stanza stanza= manager.find(bean.Stanza.class, id_stanza);
				tn.commit();
				return stanza;
			}
			
			@Override
			public void remove(Stanza s) {
				
				EntityTransaction tn=this.manager.getTransaction();
				tn.begin();
				try{
				Query q=manager.createQuery("DELETE Stanza s where s.id_stanza = ?");
				q.setParameter(1, s.getIdStanza());
				q.executeUpdate();
				}catch(NullPointerException e){
					
				}
				tn.commit();
				
			}
		    private EntityManager manager;

		



		
}
